home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / scripts / createuser < prev    next >
Encoding:
Text File  |  1992-08-27  |  3.3 KB  |  204 lines

  1. #!/bin/sh
  2. # set -x
  3. #
  4. # $Header: /private/postgres/src/scripts/RCS/createuser,v 1.3 1991/08/20 10:31:03 kemnitz Exp $
  5. #
  6. # Note - this should NOT be setuid.
  7. #
  8.  
  9. if (test -n "$POSTGRESHOME")
  10. then
  11.     PG=$POSTGRESHOME
  12. else
  13.     PG=/usr/postgres
  14. fi
  15.  
  16. #
  17. # find monitor program
  18. #
  19.  
  20. if (test -f "$PG/bin/monitor")
  21. then
  22.     MONITOR=$PG/bin/monitor
  23. elif (test -f "MONITOR=$PG/obj*/support/monitor")
  24. then
  25.     MONITOR=$PG/obj*/support/monitor
  26. else
  27.     echo "$0: can't find the monitor program!"
  28.     exit 1
  29. fi
  30.  
  31. progname=$0
  32.  
  33. if (test -n "$PGPORT")
  34. then
  35.     port=$PGPORT
  36. else
  37.     port=4321
  38. fi
  39.  
  40. if (test -n "$PGHOST")
  41. then
  42.     host=$PGHOST
  43. else
  44.     host=localhost
  45. fi
  46.  
  47. while (test -n "$1")
  48. do
  49.     case $1 in 
  50.         -h) host=$2; shift;;
  51.         -p) port=$2; shift;;
  52.          *) NEWUSER=$1;;
  53.     esac
  54.     shift;
  55. done
  56.  
  57. MARGS="-TN -h $host -p $port"
  58.  
  59. #
  60. # generate the first part of the actual monitor command
  61. #
  62.  
  63. MONITOR="$MONITOR $MARGS"
  64.  
  65. #
  66. # see if user $USER is allowed to create new users
  67. #
  68.  
  69. QUERY='retrieve (pg_user.usesuper) where pg_user.usename = '"\"$USER\""
  70.  
  71. ADDUSER=`$MONITOR -TN -c "$QUERY" template1`
  72.  
  73. if (test $? -ne 0)
  74. then
  75.     echo "$0: database access failed."
  76.     exit 1
  77. fi
  78.  
  79. if (test $ADDUSER != "t")
  80. then
  81.     echo "$0: $USER cannot create users."
  82.     exit 1
  83. fi
  84.  
  85. #
  86. # get the user name of the new user.  Make sure it doesn't already exist.
  87. #
  88.  
  89. if (test -z "$NEWUSER")
  90. then
  91.     echo -n "Enter name of user to add ---> "
  92.     read NEWUSER
  93. fi
  94.  
  95. QUERY='retrieve (pg_user.usesysid) where pg_user.usename = '"\"$NEWUSER\""
  96.  
  97. RES=`$MONITOR -TN -c "$QUERY" template1`
  98.  
  99. if (test $? -ne 0)
  100. then
  101.     echo "$0: database access failed."
  102.     exit 1
  103. fi
  104.  
  105. if (test -n "$RES")
  106. then
  107.     echo "$0: user "\"$NEWUSER\"" already exists"
  108.     exit 1
  109. fi
  110.  
  111. done=0
  112.  
  113. #
  114. # get the system id of the new user.  Make sure it is unique.
  115. #
  116.  
  117. while (test $done -ne 1)
  118. do
  119.     echo -n "Enter the user's Postgres system id ---> "
  120.     read SYSID
  121.  
  122.     QUERY='retrieve (pg_user.usename) where pg_user.usesysid = '"\"$SYSID\""
  123.     RES=`$MONITOR -TN -c "$QUERY" template1`
  124.  
  125.     if (test $? -ne 0)
  126.     then
  127.         echo "$0: database access failed."
  128.         exit 1
  129.     fi
  130.  
  131.     if (test -n "$RES")
  132.     then
  133.         echo 
  134.         echo "$0: $SYSID already belongs to $RES"
  135.         echo "The system ID must be unique."
  136.     else
  137.         done=1
  138.     fi
  139. done
  140.  
  141. #
  142. # get the rest of the user info...
  143. #
  144.  
  145. #
  146. # can the user create databases?
  147. #
  148.  
  149. yn=f
  150.  
  151. while (test "$yn" != y -a "$yn" != n)
  152. do
  153.     echo -n "Is user \"$NEWUSER\" allowed to create databases (y/n) "
  154.     read yn
  155. done
  156.  
  157. if (test "$yn" = y)
  158. then
  159.     CANCREATE=t
  160. else
  161.     CANCREATE=f
  162. fi
  163.  
  164. #
  165. # can the user add users?
  166. #
  167.  
  168. yn=f
  169.  
  170. while (test "$yn" != y -a "$yn" != n)
  171. do
  172.     echo -n "Is user \"$NEWUSER\" allowed to add users? (y/n) "
  173.     read yn
  174. done
  175.  
  176. if (test "$yn" = y)
  177. then
  178.     CANADDUSER=t
  179. else
  180.     CANADDUSER=f
  181. fi
  182.  
  183. QUERY="append pg_user (usename=\"$NEWUSER\", usesysid=$SYSID, \
  184.       usecreatedb=\"$CANCREATE\", usetrace=\"t\", usesuper=\"$CANADDUSER\",
  185.       usecatupd=\"t\")"
  186.  
  187. RES=`$MONITOR -TN -c "$QUERY" template1`
  188.  
  189. #
  190. # Wrap things up.  If the user was created successfully, AND the user was
  191. # NOT allowed to create databases, remind the DBA to create one for the user.
  192. #
  193.  
  194. if (test $? -ne 0)
  195. then
  196.     echo "$0: $NEWUSER was NOT added successfully"
  197. else
  198.     echo "$0: $NEWUSER was successfully added"
  199.     if (test $CANCREATE = f)
  200.     then
  201.         echo "don't forget to create a database for $NEWUSER"
  202.     fi
  203. fi
  204.